
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.framework.utils.UrlEncodeUtil;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;
import com.kugou.game.sdk.utils.RSAUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:验证安全问题
 *
 * @author wcj
 * @since 2015年11月5日 下午2:59:53
 */
public class CheckAnswerTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    public CheckAnswerResponse request(String userName, String answer, int questionid) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(userName);
        CheckAnswerResponse respObj = new CheckAnswerResponse();
        ResponsePackage resp = new CheckAnswerResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_CHECK_ANSWER);
        params.put("username", userName);
        params.put("answer", RSAUtil.getRASEncrypt(UrlEncodeUtil.encode(answer, "utf-8")));
        params.put("questionid", questionid);
        requestParams.add(params);
        mCmdList.add(ProtocolCmd.CMD_CHECK_ANSWER);

        req.setRequestParams(requestParams);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class CheckAnswerResponsePackage extends HttpBaseResponsePackage<CheckAnswerResponse> {

        @Override
        public void handleResponse(CheckAnswerResponse resetPasswordResponse, String jsonStr) {
            try {
                LogUtil.d("response", "CheckAnswerTask = " + jsonStr);

                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);

                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    resetPasswordResponse.setCode(code);
                    resetPasswordResponse.setMessage(message);
                    resetPasswordResponse.setPrompt(prompt);

                    if (resetPasswordResponse.isUseful()
                            && cmd.equals(ProtocolCmd.CMD_CHECK_ANSWER)) {
                        JSONObject data = jsonResp.optJSONObject("data");
                        String token = data.optString("flowtoken");
                        String unixtime = data.optString("unixtime");
                        resetPasswordResponse.setToken(token);
                        resetPasswordResponse.setUnixtime(unixtime);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 响应消息
     */
    public static class CheckAnswerResponse extends ResponseMessage {

        private String token;

        private String unixtime;

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public String getUnixtime() {
            return unixtime;
        }

        public void setUnixtime(String unixtime) {
            this.unixtime = unixtime;
        }
    }
}
